$(function() {
    $('#post-comment').hide();
    $('#btn-comment').on('click', function(event) {
        event.preventDefault();

        $('#post-comment').slideDown();
    });


    $('#btn-like').on('click', function() {
        var self = this;
        fetch('/images/' + $(self).data('id') + '/like', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({imageid: $(self).data('id')})
        })
        .then(function(response) {
            return response.json();
        })
        .then(function(obj) {
            $('strong.like-count').text(obj.likes);
        })
        .catch(function(err) {
            alert('哎呀！好像出错了？请稍后再试。')
        })
    })


    $('#btn-delete').on('click', function() {
        var remove = confirm('你真的要删除这张照片吗?');
        if (remove) {
            fetch(location.href, {method: 'DELETE'})
            .then(function(response) {
                return response.json()
            })
            .then(function(obj) {
                  $('#btn-delete').empty().text('已删除！').attr('disabled', true)
                  $('#btn-like').attr('disabled', true)
                  $('#btn-comment').attr('disabled', true)
                  $('#post-comment').attr('disabled', true)
            })
        }
    });
})
